import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:mall/constant/app_colors.dart';

import 'package:mall/constant/app_strings.dart';
import 'package:mall/constant/text_style.dart';
import 'package:mall/event/tab_select_event.dart';
import 'package:mall/gen/r.dart';
import 'package:mall/modules/sc/collect_sc.dart';
import 'package:mall/modules/sc/coupon_sc.dart';
import 'package:mall/modules/sc/foot_print_sc.dart';
import 'package:mall/modules/sc/user_sc.dart';
import 'package:mall/router/route_names.dart';
import 'package:mall/ui/widgets/divider_line.dart';
import 'package:mall/ui/widgets/icon_text_arrow_widget.dart';
import 'package:mall/router/navigator_util.dart';
import 'package:mall/utils/ext/widget_ext.dart';
import 'package:mall/utils/image_component/image_component.dart';
import 'package:mall/utils/image_component/image_preview_dialog.dart';
import 'package:mall/utils/shared_preferences_util.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class TabMinePage extends StatefulWidget {
  @override
  _TabMinePageState createState() => _TabMinePageState();
}

class _TabMinePageState extends State<TabMinePage> {
  final sc_user = Get.put(UserSc());
  final sc_coupon = Get.put(CouponListSc());
  final sc_collect = Get.put(CollectListSc());
  final sc_foot_print = Get.put(FootPrintListSc());

  @override
  Widget build(BuildContext context) {
    return _contentWidget();
  }

  Widget _contentWidget() {
    return Stack(children: [
      Container(
          height: 250 + MediaQuery.of(context).padding.top,
          decoration: BoxDecoration(
              gradient: LinearGradient(colors: [AppColors.C_FFB24E, AppColors.C_FF5722]),
              color: AppColors.C_FF5722,
              borderRadius: BorderRadius.only(bottomLeft: Radius.circular(30), bottomRight: Radius.circular(30)))),
      SafeArea(
        child: Container(
            padding: EdgeInsets.all(15),
            child: Column(children: [
              _headWidget(),
              SizedBox(height: 15),
              _userDataWidget(),
              SizedBox(height: 15),
              //订单
              _orderWidget(),
              _otherWidget(),
              Padding(padding: EdgeInsets.only(top: 45)),
              _logoutWidget()
            ])),
      )
    ]);
  }

  Widget _headWidget() {
    return Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          //头像
          InkWell(
            onTap: () => Get.dialog(ImagePreviewDialog(imgList: [sc_user.data.value?.avatarUrl], index: 0)),
            child: ImageComponent(
              sc_user.data.value?.avatarUrl,
              placeholderImage: R.image.userDefaultAvater.path,
              size: 66,
              borderRadius: BorderRadius.circular(33),
            ),
          ),
          SizedBox(width: 22),
          //名称
          Column(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Obx(() => Text(
                    sc_user.data.value?.nickName ?? "---",
                    style: TextStyle(
                      color: AppColors.C_FFFFFF,
                      fontSize: 21,
                    ),
                  )),
              Text(
                AppStrings.SUPER_VIP,
                style: FMTextStyle.color_ffffff_size_12,
              ),
            ],
          ),
        ]).onTap(() => Get.toNamed(MobileRouteNames.userInfo));
  }

  Widget _userDataWidget() {
    return Container(
      child: Row(
        children: [
          Expanded(
              flex: 1,
              child: InkWell(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Obx(() => Text(
                          "${sc_coupon.data.length}",
                          style: FMTextStyle.color_ffffff_size_13,
                        )),
                    SizedBox(height: 10),
                    Text(AppStrings.COUPON, style: FMTextStyle.color_ffffff_size_13),
                  ],
                ),
                onTap: () => NavigatorUtil.goCoupon(context),
              )),
          Expanded(
              flex: 1,
              child: InkWell(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Obx(() => Text(
                          "${sc_coupon.data.length}",
                          style: FMTextStyle.color_ffffff_size_13,
                        )),
                    SizedBox(height: 10),
                    Text(AppStrings.COLLECTION, style: FMTextStyle.color_ffffff_size_13),
                  ],
                ),
                onTap: () => NavigatorUtil.goCollect(context),
              )),
          Expanded(
              flex: 1,
              child: InkWell(
                onTap: () => NavigatorUtil.goFootPrint(context),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Text("${sc_foot_print.count}", style: FMTextStyle.color_ffffff_size_13),
                    SizedBox(height: 10),
                    Text(AppStrings.MINE_FOOTPRINT, style: FMTextStyle.color_ffffff_size_13),
                  ],
                ),
              )),
        ],
      ),
    );
  }

  Widget _orderWidget() {
    return Container(
      child: Card(
        color: AppColors.C_FFFFFF,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8))),
        child: Column(
          children: [
            Container(
              padding: EdgeInsets.only(left: 8, right: 8),
              alignment: Alignment.centerLeft,
              height: 45,
              child: Row(
                children: [
                  Text(AppStrings.MINE_ORDER, style: FMTextStyle.color_222222_size_13),
                  Expanded(
                      child: Container(
                          alignment: Alignment.centerRight,
                          child: InkWell(
                            splashColor: AppColors.C_FFFFFF,
                            highlightColor: AppColors.C_FFFFFF,
                            onTap: () => _goOrderPage(0),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.end,
                              children: [
                                Text(AppStrings.CHECK_ALL_ORDER, style: FMTextStyle.color_999999_size_12),
                                Image.asset(R.image.arrowRight.path, width: 15, height: 15),
                              ],
                            ),
                          )))
                ],
              ),
            ),
            DividerLineView(height: 0.5),
            Container(
              // margin: EdgeInsets.only(top: 15),
              height: ScreenUtil().setHeight(80),
              child: Row(
                children: [
                  Expanded(
                      flex: 1,
                      child: GestureDetector(
                        onTap: () => _goOrderPage(1),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Image.asset(
                              R.image.iconToBePaid.path,
                              width: 30,
                              height: 30,
                            ),
                            SizedBox(height: 8),
                            Text(AppStrings.TO_BE_PAID, style: FMTextStyle.color_333333_size_12),
                          ],
                        ),
                      )),
                  Expanded(
                      flex: 1,
                      child: GestureDetector(
                        onTap: () => _goOrderPage(2),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Image.asset(
                              R.image.iconToBeDelivered.path,
                              width: 30,
                              height: 30,
                            ),
                            SizedBox(height: 8),
                            Text(AppStrings.TO_BE_DELIVERED, style: FMTextStyle.color_333333_size_12),
                          ],
                        ),
                      )),
                  Expanded(
                      flex: 1,
                      child: GestureDetector(
                        onTap: () => _goOrderPage(3),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Image.asset(
                              R.image.iconToBeReceived.path,
                              width: 30,
                              height: 30,
                            ),
                            SizedBox(height: 8),
                            Text(AppStrings.TO_BE_RECEIVED, style: FMTextStyle.color_333333_size_12),
                          ],
                        ),
                      )),
                  Expanded(
                      flex: 1,
                      child: GestureDetector(
                        onTap: () => _goOrderPage(4),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Image.asset(
                              R.image.iconToBeEvaluated.path,
                              width: 30,
                              height: 30,
                            ),
                            SizedBox(height: 8),
                            Text(AppStrings.TO_BE_EVALUATED, style: FMTextStyle.color_333333_size_12),
                          ],
                        ),
                      )),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  Widget _otherWidget() {
    return Container(
        // margin: EdgeInsets.all(ScreenUtil().setWidth(AppDimens.DIMENS_10)),
        child: Card(
            color: Colors.white,
            shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8))),
            child: Container(
              padding: EdgeInsets.only(left: 10, right: 10),
              child: Column(
                children: [
                  IconTextArrowView(R.image.iconLocation.path, AppStrings.LOCATION, () => NavigatorUtil.goAddress()),
                  DividerLineView(height: 0.5),
                  IconTextArrowView(
                      R.image.iconFeedBack.path, AppStrings.FEED_BACK, () => NavigatorUtil.goFeedBack(context)),
                  DividerLineView(height: 0.5),
                  IconTextArrowView(
                      R.image.iconAboutUs.path, AppStrings.ABOUT_US, () => NavigatorUtil.goAboutUs(context)),
                ],
              ),
            )));
  }

  Widget _logoutWidget() {
    return SizedBox(
      width: double.infinity,
      height: 50,
      child: ElevatedButton(
        style: ButtonStyle(
          backgroundColor: WidgetStateProperty.all(AppColors.C_FF5722),
          shape: WidgetStateProperty.all(
            RoundedRectangleBorder(
              borderRadius: BorderRadius.all(Radius.circular(25)),
            ),
          ),
          elevation: WidgetStateProperty.all(0),
          textStyle: WidgetStateProperty.all(
            FMTextStyle.color_ffffff_size_13,
          ),
        ),
        onPressed: () => _logout(),
        child: Text(AppStrings.LOGOUT, style: TextStyle(color: AppColors.C_FFFFFF, fontSize: 16)),
      ),
    );
  }

  _goOrderPage(int initIndex) {
    NavigatorUtil.goOrderPage(context, initIndex);
  }

  _logout() {
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text(AppStrings.TIPS, style: FMTextStyle.color_333333_size_22),
            content: Text(AppStrings.CONFIRM_LOGOUT, style: FMTextStyle.color_333333_size_15),
            actions: [
              TextButton(
                  onPressed: () {
                    Navigator.pop(context);
                  },
                  child: Text(AppStrings.CANCEL, style: FMTextStyle.color_999999_size_13)),
              TextButton(
                  onPressed: () {
                    SharedPreferencesUtil.getInstance().clear().then((value) {
                      print(value);
                      if (value) {
                        Navigator.pop(context);
                        // Provider.of<UserViewModel>(context, listen: false).refreshData();
                        tabSelectBus.fire(TabSelectEvent(0));
                      }
                    });
                  },
                  child: Text(AppStrings.CONFIRM, style: FMTextStyle.color_ff5722_size_13)),
            ],
          );
        });
  }
}
